package com.eascs.admin.common.config;

import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import com.eascs.interceptor.AccessInterceptorAdapter;
import com.eascs.tag.MenuTag;
import com.eascs.tag.PermissionTag;
import com.eascs.web.DefaultWebConfig;
import com.eascs.web.sso.SsoUserInterceptor;

@Configuration
public class WebConfig extends DefaultWebConfig {
	@Bean
	public CommonsMultipartResolver multipartResolver() {
		return new org.springframework.web.multipart.commons.CommonsMultipartResolver();
	}

	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
	}

	@Override
	public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
		super.configureContentNegotiation(configurer);
	}

	@Override
	public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
		super.configureHandlerExceptionResolvers(exceptionResolvers);
	}

	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		super.configureDefaultServletHandling(configurer);
	}

	// 添加自定义拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		super.addInterceptors(registry);
		registry.addInterceptor(ssoUserInterceptor());
		registry.addInterceptor(accessInterceptorAdapter());
	}

	@Bean
	public HandlerInterceptorAdapter accessInterceptorAdapter() {
		return new AccessInterceptorAdapter();
	}

	@Bean
	public HandlerInterceptorAdapter ssoUserInterceptor() {
		return new SsoUserInterceptor();
	}

	// 设置主页
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("forward:/wf/queryTask");
	}

	@Override
	public FreeMarkerConfigurer defaultFreeMarkerConfigurer() {
		return super.defaultFreeMarkerConfigurer();
	}

	@Override
	protected Map<String, Object> buildFreemarkerVariables() {
		Map<String, Object> variables = super.buildFreemarkerVariables();
		variables.put("permission", new PermissionTag());
		variables.put("menu",new MenuTag());
		return variables;
	}
}
